<?php

namespace Home\Controller;
use Common\Controller\HomecommController;
class LogoController extends HomecommController{
    
    /**
     * 登录
     */
    public function login()
    {   
        if($_POST){
            $memberModer = D('Member');
            $username = I('post.username');
            $password = $memberModer->memberMd5(I('post.password'));
            $user = $memberModer->where(array('username'=>$username,'password'=>$password))->find();
            if($user){//登录成功 存session
                session('user_id',$user['id']);
                session('username',$user['username']);
                session('userInfo',$user);
                echo 1;
            }else{
                echo 0;
            }
        }else{
            echo 0;
        }
    }
    /**
     * 退出登录
     */
    public function logout()
    {
        //清除session
        session('user_id',null);
        session('username',null);
        session('userInfo',null);
        header('Location:'.U('Home/index/index'));
    }

    /**
     * 注册
     */
    public function register()
    {
        $phone = I('post.username');
        $password = I('post.password');
        $code = I('post.code');//手机验证码
        $memberModel = D('Member');
        $member = $memberModel->where(array('username'=>$phone))->find();
        if($member){
            echo json_encode(array('err'=>1,'msg'=>'手机号已存在'));exit;
        }
        if(!$this->checkVerify($code)){//验证 验证码
            echo json_encode(array('err'=>1,'msg'=>'验证码错误'));exit;
        }
        $data['username'] = $phone;
        $data['tel'] = $phone;
        $data['password'] = $memberModel->memberMd5($password);
        $res = $memberModel->add($data);
        if($res){
            session('user_id',$res);
            session('username',$phone);
            echo json_encode(array('err'=>0,'msg'=>'注册成功'));exit;
        }else{
            echo json_encode(array('err'=>1,'msg'=>'注册失败'));exit;
        }
    }
}
